<?php

namespace app\common\controller;

use think\Controller;
use think\Db;
use think\exception\ErrorException;
use think\Request;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-11-21
 * Time: 下午9:16
 */
class BaseWeiShop extends Controller
{

    protected $request;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
    }
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $shop = $this->request->param('shop');
        $shop = intval($shop);
        $user_info = Db::name('shop_setting')->where('seller_id',$shop)->find();
        $user_info = unserialize($user_info['set_value']);
        if(isset($_GET['state'])&&$_GET['state']=='water_shop'){
            //获取code码，登录
            try{
                $content = Authlogin($user_info);
                //通过openid判断用户是否已存在
                $where = array(
                    'member_store'=>$shop,
                    'openid'=>$content['openid']
                );
                $member_info = Db::name('shop_member')->where($where)->find();
                if(!empty($member_info)){
                    //创建session
                    $res = model('ShopMember')->createSession($member_info);
                    if($res===false){
                        throw new ErrorException('用户信息更新失败');
                    }
                }else{
                    //获取openid以后，获取用户信息
                    $member_info = AuthUser($content['access_token'],$content['openid']);
                    //获取用户信息后，创建用户并建立session
                    if(empty($member_info)){
                        throw new ErrorException('用户信息获取失败');
                    }
                    if(isset($member_info['errcode'])){
                        throw new ErrorException($member_info['errmsg']);
                    }
                    $member_info['member_store'] = $shop;
                    $member_info['parent'] = $this->request->param('parent');
                    $member_info['parent'] = intval($member_info['parent']);
                    $res = model('ShopMember')->register($member_info);
                    if($res===false){
                        throw new ErrorException('用户信息更新失败');
                    }
                }
            }catch (ErrorException $e){
                //授权登录失败,跳转到授权失败页面
                $this->redirect('watershop/Error/index',array('err'=>$e->getMessage()));
            }
        }
        if(!empty($shop)){
            $is_login = intval(session('member_id'));//判断是否登录  未登录则跳转
            if(empty($is_login)){
                //未登录，跳转授权页面登录
                $param = array(
                    'appid'=>$user_info['appid'],
                    'redirect_uri'=>GetCurUrl(),
                    'scope'=>'snsapi_userinfo ',
                    'state'=>'water_shop'
                );
                $wei_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'.http_build_query ( $param ).'#wechat_redirect';
                $this->redirect($wei_url,302);
                exit();
            }
            session('shop', $shop); // shopid
        }else{
            $this->redirect('wateradmin/Index/index');
        }
    }
}
